﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
using Charities.ModelViews;
using Charities.Forms;
using System.IO;


namespace Charities.Forms
{
    public partial class SuppliesEntriesReport : Form
    {
        int userId;
        public bool is_loaded = false;
        Form fo = null;
        public SuppliesEntriesReport(int uid, Form fm)
        {
            fo = fm;
            userId = uid;
            MdiParent = fm;
            InitializeComponent();
        }
        ModelContainer ctx = new ModelContainer();
        private void SuppliesEntriesReport_Load(object sender, EventArgs e)
        {
            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
            is_loaded = true;
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            var Batches = ctx.Batches.Where(c => c.InsertDate >= dtp_FromDate.Value && c.InsertDate <= dtp_ToDate.Value).ToList();
            List<BatchView> batchViews = new List<BatchView>();
            foreach (var batch in Batches)
            {
                BatchView b = new BatchView();
                b.Ammount = batch.Amount;
                b.DateOfEntry = batch.Entry.DateOfEntry;
                b.Description = batch.Entry.Notes;
                b.InsertUserName = ctx.Users.Single(c => c.Id == batch.InsertUId).username;
                b.Source = batch.Entry.From_;
                b.SupplyName = batch.Supply.Name;
                batchViews.Add(b);
            }
            BatchViewBindingSource.DataSource = batchViews;
            reportViewer1.LocalReport.EnableExternalImages = true;
            
            reportViewer1.LocalReport.EnableExternalImages = true;
            if (ctx.CharityInfos.Count() > 0)
            {
                CharityInfoBindingSource.DataSource = ctx.CharityInfos.First();
            }
            else
            {
                MessageBox.Show("يجب تنصيب الاعدادات الرئيسية حتى تعمل التقارير بالشكل الامثل ", "تنويه", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            this.reportViewer1.RefreshReport();

        }
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }

        private void SuppliesEntriesReport_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void SuppliesEntriesReport_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }

    }
}
